<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form" style="margin-top: 15px">
    <div class="layui-form-item" style="margin-left: 20px">
        <button class="layui-btn" lay-submit lay-filter="addBtn" id="addBtn">新增开放城市</button>
    </div>
</form>
<table class="layui-table" id="grid" lay-filter="grid">
</table>
<script th:replace="/common/inc::incJs"></script>
<script>
    var tableIns;

    $(function () {
        tableIns = createTable({
            url: 'list',
            initSort: {
                field: 'seq' //排序字段，对应 cols 设定的各字段名
                , type: 'asc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
            },
            sort: true,
            cols: [[{field: 'id', title: '编号', width: 60},
                {field: 'title', title: '标题', width: 250}
                , {field: 'cityName', title: '城市', width: 150}, {
                    field: 'contents',
                    title: '内容',
                    width: 400
                }, {field: 'status', title: '状态', width: 100, templet: '#statusTpl'}, {
                    field: 'seq',
                    title: '排序',
                    width: 100
                }, {
                    fixed: 'right',
                    title: '操作',
                    width: 200,
                    align: 'center',
                    toolbar: '#opbar'
                }
            ]], limit: 100
        });
    });

    layui.table.on('tool(grid)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象02\
        if (layEvent === 'change') { //删除
            changeStatus(data.id, data.status);
        } else if (layEvent === 'edit') { //编辑
            openDialog({
                url: "/backend/security/openCity/detail?id=" + data.id,
                area: ["70%", "80%"]
            });
        }
    });

    //变更开放城市状态
    function changeStatus(cityId, status) {
        if (status == 0) {
            $.post('/backend/security/openCity/update', {id: cityId, status: 1}, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg("上架成功");
                        tableIns.reload();
                    }
                }
            )
        } else if (status = 1) {
            $.post('/backend/security/openCity/update', {id: cityId, status: 0}, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg("下架成功");
                        tableIns.reload();
                    }
                }
            )
        }
    }

    layui.form.on('submit(addBtn)', function () {
        openDialog({
            url: "/backend/security/openCity/detail",
            area: ["70%", "80%"]
        });
        return false;
    });

    layui.form.on('submit(searchBtn)', function (data) {
        tableIns.reload({
            where: data.field
        });
        return false;
    });

</script>
<script type="text/html" id="statusTpl">
    {{#  if(d.status ===0){ }}
    <span style="color: #FF0000;">下架</span>
    {{#  } else { }}
    <span style="color: green;">上架</span>
    {{#  } }}
</script>
<script type="text/html" id="opbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="change">变更状态</a>
</script>
</body>
</html>
